#include "TcpServer.hpp"
#include "Protocol.hpp"
#include "NetCal.hpp"
#include "Daemon.hpp"

std::string defaultfunc(const std::string &word, InetAddr &addr)
{
    LOG(LogLevel::DEBUG) << "defaultfunc";
    return "hello " + word;
}

void Usage(const std::string proc)
{
    std::cerr << "Usage:" << proc << " port" << std::endl;
}

int main(int args, char* argv[])
{
    if(args != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    
    std::cout << "服务器已启动" << std::endl;
    // Daemon(1, 0);
    daemon(1, 0); // 系统调用 1.为1不更改路径,反之更改路径到/ 2.为0重定向标准输入输出错误,反之不处理

    // Enable_Console_Log_Strategy();
    Enable_File_Log_Strategy();

    // 顶层
    std::unique_ptr<Cal>cal = std::make_unique<Cal>();
    // 协议层
    std::unique_ptr<Protocol> pro = std::make_unique<Protocol>([&cal](Request &req)->Response{
        return cal->Execute(req);
    });
    // 服务器层
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(std::stoi(argv[1]), 
        [&pro](std::shared_ptr<Socket> &socket, InetAddr &client){
            pro->GetRequest(socket, client);
    });

    tsvr->Start();
    return 0;
}